package com.wrxdz.start.surfaceviewt.surfview.utils;

import android.graphics.Rect;

public class Utils {

    /**
     * 对应扑克所在图片上的位置
     * 1 5 9 ………… 53
     * 2 6 10 ………… 54
     * 3 7 11
     * 4 8 12
     */
    public static Rect cardRect(int cardValue, int width, int height) {
        int x, y;
        if (cardValue % 4 == 0) {
            x = cardValue / 4 - 1;
            y = 4;
        } else {
            x = cardValue / 4;
            y = cardValue % 4;
        }
        int left = x * width;
        int top = (y - 1) * height;
        int right = (x + 1) * width;
        int bottom = (y) * height;
        return new Rect(left, top, right, bottom);
    }

    /**
     * int[] to String
     *
     * @param a
     * @return
     */
    public static String ArrayIntToString(int[] a) {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < a.length - 1; i++) {
            s.append(a[i]).append("-");
        }
        s.append(a[a.length - 1]);
        return s.toString();
    }

    /**
     * String to int[]
     *
     * @param s
     * @return
     */
    public static int[] StringToArrayInt(String s) {
        String[] strings = s.split("-");
        int[] a = new int[strings.length];
        for (int i = 0; i < strings.length; i++) {
            a[i] = Integer.valueOf(strings[i]);
        }
        return a;
    }

    /**
     * 出牌是否符合出牌逻辑
     * Que: 出牌索引顺序有问题，牌数 3,4 的下标索引为 0,1( (putPukes[i] - 1 ) / 4) , 顺子判断存在问题
     *
     * @param putPukes 当前要出的牌
     * @return true 可以出牌 false 不可以出牌
     */
    public static boolean accordPutLogic(final int[] putPukes) {
        int[] result = initResult(putPukes);
        switch (putPukes.length) {
            //单张
            case 1:
                return true;
            //对子或王炸
            case 2:
                //判断是否在同一列
                return result[0] == result[1];
            //三张不带
            case 3:
                return result[0] == result[1] && result[1] == result[2];
            //炸弹或三带一
            case 4:
                return (result[0] == result[1] && result[1] == result[2] && result[2] == result[3])
                        || ((result[0] == result[1] && result[1] == result[2]) || (result[1] == result[2] && result[2] == result[3]));
            //顺子或三带一对
            case 5:
                return  // 三带一对
                        ((result[0] == result[1] && result[1] == result[2] && result[3] == result[4]) || (result[2] == result[3] && result[3] == result[4] && result[0] == result[1]))
                                //顺子
                                || (result[4] < 12 && result[0] == result[1] - 1 && result[1] == result[2] - 1 && result[2] == result[3] - 1 && result[3] == result[4] - 1);
            //顺子或飞机不带或四带二或连对
            case 6:
                return  // 顺子
                        (result[5] < 12 && result[0] == result[1] - 1 && result[1] == result[2] - 1 && result[2] == result[3] - 1 && result[3] == result[4] - 1 && result[4] == result[5] - 1)
                                //飞机不带
                                || (result[0] == result[1] && result[1] == result[2] && result[3] == result[4] && result[4] == result[5])
                                //四带二
                                || ((result[0] == result[1] && result[2] == result[3] && result[3] == result[4] && result[4] == result[5]) || (result[4] == result[5] && result[0] == result[1] && result[1] == result[2] && result[2] == result[3]))
                                //连对
                                || (result[4] < 12 && result[0] == result[1] && result[2] == result[3] && result[4] == result[5]);
            //顺子
            case 7:
                return (result[6] < 12 && result[0] == result[1] - 1 && result[1] == result[2] - 1 && result[2] == result[3] - 1 && result[3] == result[4] - 1 && result[4] == result[5] - 1 && result[5] == result[6] - 1);
            //顺子或或连对
            case 8:
                return //顺子
                        (result[7] < 12 && result[0] == result[1] - 1 && result[1] == result[2] - 1 && result[2] == result[3] - 1 && result[3] == result[4] - 1 && result[4] == result[5] - 1 && result[5] == result[6] - 1 && result[6] == result[7] - 1)
                                //连对
                                || (result[6] < 12 && result[0] == result[1] && result[2] == result[3] && result[4] == result[5] && result[6] == result[7]);
            //顺子
            case 9:
                return (result[8] < 12 && result[0] == result[1] - 1 && result[1] == result[2] - 1 && result[2] == result[3] - 1 && result[3] == result[4] - 1 && result[4] == result[5] - 1 && result[5] == result[6] - 1 && result[6] == result[7] - 1 && result[7] == result[8] - 1);
            //顺子或飞机带两对或连对
            case 10:
                return //顺子
                        (result[9] < 12 && result[0] == result[1] - 1 && result[1] == result[2] - 1 && result[2] == result[3] - 1 && result[3] == result[4] - 1 && result[4] == result[5] - 1 && result[5] == result[6] - 1 && result[6] == result[7] - 1 && result[7] == result[8] - 1 && result[8] == result[9] - 1)
                                //飞机带两对
                                || (
                                //3322
                                (result[0] == result[3] && result[0] == result[1] && result[1] == result[2] && result[3] == result[4] && result[4] == result[5] && result[6] == result[7] && result[8] == result[9])
                                        //2233
                                        || (result[4] == result[7] && result[4] == result[5] && result[5] == result[6] && result[7] == result[8] && result[8] == result[9] && result[0] == result[1] && result[2] == result[3]))
                                //连对
                                || (result[8] < 12 && result[0] == result[1] && result[2] == result[3] && result[4] == result[5] && result[6] == result[7] && result[8] == result[9]);
            //顺子
            case 11:
                return (result[10] < 12 && result[0] == result[1] - 1 && result[1] == result[2] - 1 && result[2] == result[3] - 1 && result[3] == result[4] - 1 && result[4] == result[5] - 1 && result[5] == result[6] - 1 && result[6] == result[7] - 1 && result[7] == result[8] - 1 && result[8] == result[9] - 1 && result[9] == result[10] - 1);
            //顺子或连对
            case 12:
                return //顺子
                        (result[11] < 12 && result[0] == result[1] - 1 && result[1] == result[2] - 1 && result[2] == result[3] - 1 && result[3] == result[4] - 1 && result[4] == result[5] - 1 && result[5] == result[6] - 1 && result[6] == result[7] - 1 && result[7] == result[8] - 1 && result[8] == result[9] - 1 && result[9] == result[10] - 1 && result[10] == result[11] - 1)
                                //连对
                                || (result[10] < 12 && result[0] == result[1] && result[2] == result[3] && result[4] == result[5] && result[6] == result[7] && result[8] == result[9] && result[10] == result[11]);
        }
        return true;
    }

    /**
     * 获取牌数索引
     *
     * @param a 54张牌其中一张
     * @return 图片的某一列数
     * 3 4 5 6 7 8 9 10 J Q K 1 2 鬼
     * 0 1 2 3 4 5 6 7 8 9 10 11 12 13
     */
    private static int getIndex(int a) {
        return (a - 1) / 4;
    }

    private static int[] initResult(final int[] a) {
        int[] result = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            result[i] = getIndex(a[i]);
        }
        return result;
    }
}
